home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Utilities Professional 1-1500
/
Utilities Professional 1-1500 (1994)(WPD)[!].iso
/
12511500
/
var1449.dms
/
var1449.adf
/
TimerDevice
/
Example4.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-27
|
7KB
|
213 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Timer Device Tulevagen 22 */
/* File: Example4.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-27 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This example demonstrates how you can compare, add and */
/* subtract timevalues with help of the timer device's own */
/* functions. */
#include <exec/types.h> /* STRPTR */
#include <exec/ports.h> /* struct Message */
#include <exec/memory.h> /* MEMF_PUBLIC */
#include <devices/timer.h> /* TIMERNAME */
struct Device *TimerBase;
/* The reply port: */
struct MsgPort *replymp;
/* The timer request blocks: */
struct timerequest *tr1;
struct timerequest *tr2;
struct timerequest *tr3;
/* When the Timer Device is open this variable is TRUE: */
BOOL not1_opened = TRUE;
BOOL not2_opened = TRUE;
BOOL not3_opened = TRUE;
/* Declare our functions: */
void clean_up();
void main();
void main()
{
/* Get a reply port: */
replymp = (struct MsgPort *)
CreatePort( NULL, 0 );
if( !replymp )
clean_up( "Could not create the reply port!" );
/* Get three extended request blocks: */
/* Requestblock 1: */
tr1 = (struct timerequest *)
CreateExtIO( replymp, sizeof(struct timerequest) );
if( !tr1 )
clean_up( "Could not create requestblock 1!" );
/* Requestblock 2: */
tr2 = (struct timerequest *)
CreateExtIO( replymp, sizeof(struct timerequest) );
if( !tr2 )
clean_up( "Could not create requestblock 2!" );
/* Requestblock 3: */
tr3 = (struct timerequest *)
CreateExtIO( replymp, sizeof(struct timerequest) );
if( !tr3 )
clean_up( "Could not create requestblock 3!" );
/* Open the Timer Device: */
not1_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr1 ,0 );
if( not1_opened )
clean_up( "Could not open the Timer Device (1)!" );
not2_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr2 ,0 );
if( not2_opened )
clean_up( "Could not open the Timer Device (2)!" );
not3_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr3 ,0 );
if( not3_opened )
clean_up( "Could not open the Timer Device (3)!" );
/* Set our request: */
/* Requestblock 1: */
tr1->tr_node.io_Command = TR_ADDREQUEST; /* Add a request. */
tr1->tr_time.tv_secs = 10; /* 10 seconds. */
tr1->tr_time.tv_micro = 0; /* 0 micro seconds. */
/* Requestblock 2: */
tr2->tr_node.io_Command = TR_ADDREQUEST; /* Add a request. */
tr2->tr_time.tv_secs = 7; /* 7 seconds. */
tr2->tr_time.tv_micro = 0; /* 0 micro seconds. */
/* Requestblock 3: */
tr3->tr_node.io_Command = TR_ADDREQUEST; /* Add a request. */
tr3->tr_time.tv_secs = 5; /* 5 seconds. */
tr3->tr_time.tv_micro = 0; /* 0 micro seconds. */
/* Get a pointer to the timer device: */
TimerBase = tr1->tr_node.io_Device;
/* Compare the first two time val structures: */
/* (Since the first timeval structure is greater */
/* than the second timeval structure -1 should */
/* be returned. */
printf( "Compare time1 - time2: %d\n",
CmpTime( &(tr1->tr_time), &(tr2->tr_time) ) );
/* Compare the same timeval structures, but put */
/* them in the opposite position: (1 should be */
/* returned.) */
printf( "Compare time2 - time1: %d\n",
CmpTime( &(tr2->tr_time), &(tr1->tr_time) ) );
/* Add the time in the third timeval structure to */
/* the second timeval structure. The second timeval */
/* structure should now contain the time 12 sec. */
printf( "Add time3 with time2, and store the result in time2.\n" );
AddTime( &(tr2->tr_time), &(tr3->tr_time) );
/* Compare the first time val structure with the */
/* second timeval structure again. The second */
/* structure should now be greater, and thus 1 */
/* should be returned. */
printf( "Compare time1 - time2: %d\n",
CmpTime( &(tr1->tr_time), &(tr2->tr_time) ) );
/* Subtract the time in the third timeval structure with */
/* the second timeval structure. The second timeval structure */
/* should now contain the time 7 seconds again. */
printf( "Subtract time3 with time2, and store the result in time2.\n" );
SubTime( &(tr2->tr_time), &(tr3->tr_time) );
/* Compare the first time val structure with the */
/* second timeval structure again. The first */
/* structure should now be greater again, and */
/* thus 1 should be returned. */
printf( "Compare time1 - time2: %d\n",
CmpTime( &(tr1->tr_time), &(tr2->tr_time) ) );
/* We prepare three time requestblocks as normal, but in */
/* this simple example we do not use them. We simply use */
/* the timeval structures. */
/* Clean up and quit: */
clean_up( "The End!" );
}
void clean_up( text )
STRPTR text;
{
/* Close the Timer Devices: */
if( !not1_opened )
CloseDevice( tr1 );
if( !not2_opened )
CloseDevice( tr2 );
if( !not3_opened )
CloseDevice( tr3 );
/* Delete the extended request blocks: */
if( tr1 ) DeleteExtIO( tr1, sizeof( struct timerequest) );
if( tr2 ) DeleteExtIO( tr2, sizeof( struct timerequest) );
if( tr3 ) DeleteExtIO( tr3, sizeof( struct timerequest) );
/* Remove the message port: */
if( replymp )
DeletePort( replymp );
/* Print the last message: */
printf( "%s\n", text );
/* Quit: */
exit( 0 );
}